﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace QuanLyThietBi
{
    class CXuly_phieunhap
    {
        SqlConnection con;
        public CXuly_phieunhap(SqlConnection con)
        {
            this.con = con;
        }
        public List<CPhieunhap> getdsphieunhap()
        {
            con.Open();
            string str = "select * from phieunhap";
            SqlCommand com = new SqlCommand(str, con);
            SqlDataReader data = com.ExecuteReader();
            List<CPhieunhap> list = new List<CPhieunhap>();
            while (data.Read())
            {
                CPhieunhap p = new CPhieunhap();
                p.Maphieunhap = data["maphieunhap"].ToString();
                p.Ngaynhap = DateTime.Parse(data["ngaynhap"].ToString());
                p.Tennguoinhap = data["tennguoinhap"].ToString();

                list.Add(p);

            }
            con.Close();
            foreach (CPhieunhap x in list)
            {
                x.Chitietphieunhap = getdschitietphieunhap(x.Maphieunhap);
            }
            return list;

        }
        public List<CPhieunhap> getdsphieunhap(int trangthai)
        {
            con.Open();
            string str = "select * from phieunhap where trangthai=@trangthai";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@trangthai", trangthai);
            SqlDataReader data = com.ExecuteReader();
            List<CPhieunhap> list = new List<CPhieunhap>();
            while (data.Read())
            {
                CPhieunhap p = new CPhieunhap();
                p.Maphieunhap = data["maphieunhap"].ToString();
                p.Ngaynhap = DateTime.Parse(data["ngaynhap"].ToString());
                p.Tennguoinhap = data["tennguoinhap"].ToString();

                list.Add(p);

            }
            con.Close();
            foreach (CPhieunhap x in list)
            {
                x.Chitietphieunhap = getdschitietphieunhap(x.Maphieunhap);
            }
            return list;

        }
        public List<CChitietphieunhap> getdschitietphieunhap(string maphieunhap)
        {
            List<CChitietphieunhap> list = new List<CChitietphieunhap>();
            con.Open();
            string str = "select * from chitietphieunhap where maphieunhap=@maphieunhap";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@maphieunhap", maphieunhap);
            SqlDataReader data = com.ExecuteReader();
            while (data.Read())
            {
                CChitietphieunhap x = new CChitietphieunhap();
                x.Maphieunhap = data["maphieunhap"].ToString();
                x.Vattu.Mavattu = data["mavattu"].ToString();
                x.Soluong = int.Parse(data["soluong"].ToString());
                list.Add(x);
            }
            con.Close();
            return list;
        }
        public CPhieunhap tim(string maphieunhap)
        {
            CPhieunhap p = null;
            con.Open();
            string str = "select * from phieunhap where maphieunhap=@maphieunhap";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@maphieunhap", maphieunhap);
            SqlDataReader data = com.ExecuteReader();
            while (data.Read())
            {
                p = new CPhieunhap();
                p.Maphieunhap = data["maphieunhap"].ToString();
                p.Ngaynhap = DateTime.Parse(data["ngaynhap"].ToString());
                p.Tennguoinhap = data["tennguoinhap"].ToString();
                p.Trangthai = int.Parse(data["trangthai"].ToString());
            }
            con.Close();
            if (p == null) return p;
            else p.Chitietphieunhap = getdschitietphieunhap(p.Maphieunhap);
            return p;
        }
        public bool them(CPhieunhap p)
        {
            if (tim(p.Maphieunhap) != null) return false;
            con.Open();
            string str = "insert into phieunhap values(@maphieunhap,@ngaynhap,@tennguoinhap,@trangthai)";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@maphieunhap", p.Maphieunhap);
            com.Parameters.AddWithValue("@ngaynhap", p.Ngaynhap);
            com.Parameters.AddWithValue("@tennguoinhap", p.Tennguoinhap);
            com.Parameters.AddWithValue("@trangthai", 0);
            com.ExecuteNonQuery();
            con.Close();
            foreach (CChitietphieunhap x in p.Chitietphieunhap)
            {
                themchitietphieunhap(x);
            }
            return true;
        }
        public void themchitietphieunhap(CChitietphieunhap x)
        {
            con.Open();
            string str = "insert into chitietphieunhap values(@maphieunhap,@mavattu,@soluong)";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@maphieunhap", x.Maphieunhap);
            com.Parameters.AddWithValue("@mavattu", x.Mavattu);
            com.Parameters.AddWithValue("@soluong", x.Soluong);

            com.ExecuteNonQuery();
            con.Close();
        }
        public void sua(CPhieunhap p)
        {

            con.Open();
            string str = "update phieunhap set ngaynhap=@ngaynhap,tennguoinhap=@tennguoinhap where maphieunhap=@maphieunhap";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@maphieunhap", p.Maphieunhap);
            com.Parameters.AddWithValue("@ngaynhap", p.Ngaynhap);
            com.Parameters.AddWithValue("@tennguoinhap", p.Tennguoinhap);
            com.ExecuteNonQuery();
            con.Close();
            xoachitietphieunhap(p.Maphieunhap);
            foreach (CChitietphieunhap x in p.Chitietphieunhap)
            {
                suachitietphieunhap(x);
            }
        }
        public void suachitietphieunhap(CChitietphieunhap x)
        {
            themchitietphieunhap(x);
        }
        public void xoa(string maphieunhap)
        {
            xoachitietphieunhap(maphieunhap);
            con.Open();
            string str = "delete from phieunhap where maphieunhap=@maphieunhap";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@maphieunhap", maphieunhap);
            com.ExecuteNonQuery();
            con.Close();
        }
        public void xoachitietphieunhap(string maphieunhap)
        {
            con.Open();
            string str = "delete from chitietphieunhap where maphieunhap=@maphieunhap";
            SqlCommand com = new SqlCommand(str, con);
            com.Parameters.AddWithValue("@maphieunhap", maphieunhap);
            com.ExecuteNonQuery();
            con.Close();
        }
    }
}
